package com.edgclub.edg.activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.edgclub.edg.R;
import com.edgclub.edg.adapter.GzGuideViewPagerAdapter;
import com.edgclub.edg.utils.util.BitmapUtility;
import com.edgclub.edg.utils.util.CommAppPrefUtils;
import com.zhzhinn.activity.ZZLoginActivity;

import java.util.ArrayList;
import java.util.List;

public class GzGuideActivity extends GzBaseActivity implements OnClickListener,
		OnPageChangeListener {

	private ViewPager vp;
	private GzGuideViewPagerAdapter vpAdapter;
	private List<View> views;


	// 引导图片资源
	private static final int[] pics = { R.drawable.guide1,
			R.drawable.guide2, R.drawable.guide3,R.drawable.guide4};

	// 底部小店图片
	private ImageView[] dots;

	// 记录当前选中位置
	private int currentIndex;

	boolean first;

	private ImageView gotoapp;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gz_guide);

		views = new ArrayList<View>();
		gotoapp = (ImageView) findViewById(R.id.gotoapp);
		gotoapp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				GzGuideActivity.this.startActivity(new Intent(
						GzGuideActivity.this, ZZLoginActivity.class));
				GzGuideActivity.this.finish();

			}
		});

		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		// 初始化引导图片列表
		for (int i = 0; i < pics.length; i++) {
			ImageView iv = new ImageView(this);
			iv.setLayoutParams(mParams);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
				iv.setBackground(new BitmapDrawable(BitmapUtility.readBitmap(
						this, pics[i])));

			} else {
				iv.setBackgroundDrawable(new BitmapDrawable(BitmapUtility
						.readBitmap(this, pics[i])));
			}
			views.add(iv);
		}

		vp = (ViewPager) findViewById(R.id.viewpager);
		// 初始化Adapter
		vpAdapter = new GzGuideViewPagerAdapter(views, this, first);
		vp.setAdapter(vpAdapter);
		// 绑定回调
		vp.setOnPageChangeListener(this);
		new CommAppPrefUtils(this).saveGuide();
		vp.setOnClickListener(this);
		// 初始化底部小点
//		initDots();

	}

	private void initDots() {
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

		dots = new ImageView[pics.length];

		// 循环取得小点图片
		for (int i = 0; i < pics.length; i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);// 都设为灰色
			dots[i].setOnClickListener(this);
			dots[i].setTag(i);// 设置位置tag，方便取出与当前位置对应
		}

		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
	}

	// 设置当前的引导页
	private void setCurView(int position) {
		if (position < 0 || position >= pics.length) {
			return;
		}

		vp.setCurrentItem(position);
	}

	// 这只当前引导小点的选中
	private void setCurDot(int position) {
//		if (position < 0 || position > pics.length - 1
//				|| currentIndex == position) {
//			return;
//		}
//
//		dots[position].setEnabled(false);
//		dots[currentIndex].setEnabled(true);

		currentIndex = position;
		if (position == 3) {
			gotoapp.setVisibility(View.VISIBLE);
		} else {
			gotoapp.setVisibility(View.GONE);
		}
	}

	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	// 当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	// 当新的页面被选中时调用
	@Override
	public void onPageSelected(int arg0) {
		// 设置底部小点选中状态
		setCurDot(arg0);

	}

	@Override
	public void onClick(View v) {
		if (R.id.ll == ((View) v.getParent()).getId()) {
			int position = (Integer) v.getTag();
			setCurView(position);
			setCurDot(position);
		}
	}
}